package com.hl.springboot4.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.springboot4.entity.Article;
import com.hl.springboot4.entity.ArticleType;
import com.hl.springboot4.mapper.ArticleMapper;
import com.hl.springboot4.service.ArticleService;
import com.hl.springboot4.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ArticleServiceImpl
 * @Description TODO
 * @Author libowen
 * @Date 2025年03月03日 10:08
 * @Version 1.0
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTypeService articleTypeService;
    @Override
    public int insert(Article article) {
        return articleMapper.insert(article);
    }

    @Override
    public int delete(int id) {
        return articleMapper.delete(id);
    }

    @Override
    public int update(Article article) {
        return articleMapper.update(article);
    }

    @Override
    public Article selectById(int id) {
        return articleMapper.selectById(id);
    }

    @Override
    public PageInfo<Article> selectAll(int pageNum, int pageSize, Article article) {
        // 分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<Article> list = articleMapper.selectAll(article);
        // 一对一，根据文章的tid查询文章类型
        for (Article article1 : list) {
            ArticleType articleType = articleTypeService.findById(article1.getTid());
            article1.setArticleType(articleType);
        }
        // 封装
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
